import express from 'express'
import { ResponseCode, RouterPathMap } from '../utils/stateEnum/index'
import { urlencoded } from 'body-parser'
import { LoginUser } from '../utils/module/index'
import { getCurrentDir } from '../getFileArr'
import { formatFileNameArr, packViewsFolderTreeData, ElTreeComponentDataType } from '../utils/function'

const router = express.Router()

const parseUrlencoded = urlencoded({ extended: true }) // 解析application/x-www-form-urlencoded

/***  全局根路由 ***/
router.get('/', (req, res) => {
    res.setHeader('Content-type', 'text/html; charset=utf-8')
    res.send('<h3>Hello Express</h3>')
    return
})

/* v1路由组  */
{
    router.all('/v1/*', (req, res, next) => {
        /*** 处理跨域 ***/
        res.setHeader('Access-Control-Allow-Origin', '*')
        res.setHeader('Access-Control-Allow-Methods', 'GET, POST')
        res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Accept, X-Access-Token') // X-Access-Token => 处理 options 请求！！！ 不然后续带有 X-Access-Token 的任何请求将无法处理
        res.setHeader('Content-Type', 'Application/json;charset=UTF-8')
        next()
    })

    /** 登录接口 **/
    // router.post(RouterPathMap.Login, parseUrlencoded, (req, res) => {
    router.post(RouterPathMap.Login, (req, res) => {
        console.log('req:', req.body)
        // const user: LoginUser = {
        //     name: req.body.name || 'default',
        //     passwd: req.body.passwd || '123456',
        // }
        res.send({
            code: ResponseCode.Ok,
            data: { accessToken: 'admin-token', },
            msg: '登录成功'
        })
        return
    })

    /** 用户信息接口 **/
    router.post(RouterPathMap.Info, (req, res) => {
        console.log('userInfo:', req.body)
        res.send({
            code: ResponseCode.Ok,
            data: {
                user: {
                    id: 0,
                    username: "admin",
                    password: "any",
                    name: "Super Admin",
                    avatar: "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif",
                    introduction: "I am a super administrator",
                    email: "admin@test.com",
                    phone: "1234567890",
                    roles: ["admin"]
                }
            },
            msg: '信息获取成功'
        })
    })

    /** 用户退出接口 **/
    router.post(RouterPathMap.Logout, (req, res) => {
        console.log('req:', req.body)
        res.send({
            code: ResponseCode.Ok,
            msg: '喜欢您来~~'
        })
        return
    })

    /** 前端路由的增删改查 **/
    {
        /** 前端路由的查询 **/
    }

    /** 获取前端的文件目录 **/
    router.get(RouterPathMap.FileTree, (req, res) => {
        getCurrentDir().then(fileNameArr => {
            console.log('origin:', fileNameArr)
            const formatArr = formatFileNameArr(fileNameArr)
            const ElTreeComponentData = packViewsFolderTreeData(formatArr)
            console.log('ElTreeComponentData:', ElTreeComponentData)
            res.send({
                code: ResponseCode.Ok,
                data: {
                    frontendPageFileList: [<ElTreeComponentDataType>{ label: 'views', children: ElTreeComponentData }],
                },
                msg: '目录获取成功'
            })
        })
    })
}


export default router